<template>
	<view class="page">
		<!-- 顶部背景 + 导航 -->
		<image src="/static/dingbu.png" class="mine-navbar-bg" :style="{ height: navbarHeight + 'rpx' }"
			mode="aspectFill" />
		<!-- 标题改为详情 -->
		<u-navbar title="抽样单" :autoBack="true" leftIconColor="#fff" bgColor="transparent" :titleStyle="{ color: '#fff' }"
			safeAreaInsetTop placeholder />

		<!-- 任务信息卡片 -->
		<view class="task-info-card">
			<view class="task-info-header">
				<u-icon name="file-text" color="#67C23A" size="18"></u-icon>
				<text class="task-info-title">任务信息</text>
			</view>
			<view class="task-info-content">
				<view class="info-item">
					<view class="info-label">
						<u-icon name="file-text" color="#5a9f3a" size="14"></u-icon>
						<text>任务名称</text>
					</view>
					<view class="info-value">{{ taskInfo.taskName || '-' }}</view>
				</view>
				<view class="info-divider"></view>
				<view class="info-item">
					<view class="info-label">
						<u-icon name="home" color="#5a9f3a" size="14"></u-icon>
						<text>下发机构</text>
					</view>
					<view class="info-value">{{ taskInfo.issueDeptName || '-' }}</view>
				</view>
				<view class="info-divider"></view>
				<view class="info-item">
					<view class="info-label">
						<u-icon name="map" color="#5a9f3a" size="14"></u-icon>
						<text>任务路径</text>
					</view>
					<view class="info-value">{{ taskInfo.taskPath || '-' }}</view>
				</view>
				<view class="info-divider"></view>
				<view class="info-item">
					<view class="info-label">
						<u-icon name="list" color="#5a9f3a" size="14"></u-icon>
						<text>任务数量</text>
					</view>
					<view class="info-value">{{ taskInfo.taskCount || 0 }}</view>
				</view>
			</view>
		</view>

		<view class="w-710 mgt bgff pl-32 pr-32 br-20 pt-20 pb-20" v-for="(item, index) in list" :key="index">
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">采样编号</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.simplingCode}}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">监测场所</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.simplingPlace }}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">采样地点</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.product}}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">受检单位</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.testedName}}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">联系人</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.contact }}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">联系电话</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.contactPhone }}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">采样时间</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.simplingTime }}</view>
			</view>
			<view class="rows rowsm h-58">
				<view class="fs-28 fw-500 col888 label">采样人员</view>
				<view class="fs-28 fw-500 col888 mr-10 ml-10">:</view>
				<view class="">{{ item.simplingPerson }}</view>
			</view>

			<view style="display: flex;align-items: center;justify-content: flex-end;">
				<view class="w-132 h-52 colfff bg rowsc rowsm br-32" @click="goEdit(item)">编辑</view>
			</view>
		</view>

	</view>
</template>

<script>
	import {
		trcylistAll,
		getTask
	} from '@/api/sylin/index.js'
	export default {
		data() {
			return {
				list: [],
				loading: false,
				receiveId: null,
				taskInfo: {
					id: null,
					taskName: '',
					issueDeptName: '',
					taskPath: '',
					taskCount: 0
				}
			}
		},
		onLoad(options) {
			// 从URL参数获取receiveId和任务信息
			this.receiveId = options.id
			this.taskInfo.id = options.taskId || null
			
			// 如果有taskId，则获取任务详细信息
			if (this.taskInfo.id) {
				this.loadTaskInfo()
			} else {
				// 使用默认值
				this.taskInfo.taskName = options.taskName || '编制抽样单'
				this.taskInfo.issueDeptName = options.issueDeptName || '-'
				this.taskInfo.taskPath = options.taskPath || '-'
				this.taskInfo.taskCount = parseInt(options.taskCount) || 0
			}
			
			if (this.receiveId) {
				this.gettrcylistAll(this.receiveId)
			}
		},
		methods: {
			// 加载任务信息
			async loadTaskInfo() {
				if (!this.taskInfo.id) {
					console.warn('任务ID为空，无法获取任务信息')
					return
				}
				
				this.loading = true
				try {
					const response = await getTask(this.taskInfo.id)
					console.log('任务信息API响应:', response)
					
					if (response.code === 200 && response.data) {
						const taskData = response.data
						console.log('任务数据:', taskData)
						
						// 填充任务基本信息
						this.taskInfo = {
							...this.taskInfo,
							taskName: taskData.instanceName || '编制抽样单',
							issueDeptName: taskData.data?.processVariables?.inputDeptName || '-',
							taskPath: this.buildTaskPath(taskData),
							taskCount: taskData.data?.processVariables?.countyDept?.taskQuantity || 0
						}
						
						console.log('填充后的任务信息:', this.taskInfo)
						
						// 提取receiveId
						if (taskData.data?.processVariables?.countyDept?.receiveId) {
							this.receiveId = taskData.data.processVariables.countyDept.receiveId
							// 重新加载抽样单列表
							this.gettrcylistAll(this.receiveId)
						}
						
						uni.showToast({
							title: '任务信息加载成功',
							icon: 'success'
						})
					} else {
						uni.showToast({
							title: response.msg || '获取任务信息失败',
							icon: 'error'
						})
					}
				} catch (error) {
					console.error('获取任务信息失败:', error)
					uni.showToast({
						title: '获取任务信息失败',
						icon: 'error'
					})
				} finally {
					this.loading = false
				}
			},
			
			// 构建任务路径
			buildTaskPath(taskData) {
				try {
					const pv = taskData.data?.processVariables
					if (!pv) return '-'
					
					const parts = []
					
					// 添加任务下发机构
					if (pv.inputDeptName) {
						parts.push(pv.inputDeptName)
					}
					
					// 添加市级名称
					if (pv.municipalDept?.cityName) {
						parts.push(pv.municipalDept.cityName)
					}
					
					// 添加县级名称
					if (pv.countyDept?.countyName) {
						parts.push(pv.countyDept.countyName)
					}
					
					return parts.length > 0 ? parts.join(' > ') : '-'
				} catch (error) {
					console.error('构建任务路径失败:', error)
					return '-'
				}
			},
			
			async gettrcylistAll(receiveId) {
				const res = await trcylistAll({
					receiveId
				})
				console.log(res)
				this.list = res.data
			},
			goEdit(item) {
				// 传递完整的抽样单数据，包括simplingCode、taskSource、monitorType等
				const params = {
					id: item.id,
					simplingCode: item.simplingCode || '',
					taskSource: item.taskSource || '',
					monitorType: item.monitorType || ''
				}
				
				// 构建URL参数
				const queryString = Object.keys(params)
					.map(key => `${key}=${encodeURIComponent(params[key])}`)
					.join('&')
				
				uni.navigateTo({
					url: `/pages/trchouyangdan/edit?${queryString}`
				})
			}
		},
		computed: {
			navbarHeight() {
				// 如果设置了自定义高度，则使用自定义高度
				if (this.customNavbarHeight) {
					return this.customNavbarHeight;
				}
				// 否则自动计算：状态栏高度 + 导航栏高度
				const systemInfo = uni.getSystemInfoSync();
				// 将px转换为rpx (1px = 2rpx)
				const statusBarHeightRpx = systemInfo.statusBarHeight * 2;
				const navbarHeightRpx = 44 * 2; // 44px = 88rpx
				return statusBarHeightRpx + navbarHeightRpx;
			}
		}
	}
</script>

<style lang="scss">
	$green: #0A993B;
	$green-2: #23a44b;
	$line: #E5F3EA;
	$gray: #999;
	$primary: #67C23A;
	$success: #67C23A;
	$info: #909399;
	$warning: #E6A23C;
	$danger: #F56C6C;

	.page {
		background: #f5f7fa;
		min-height: 100vh;
		padding: 20rpx;
	}

	.mine-navbar-bg {
		position: fixed;
		top: 0;
		left: 0;
		width: 750rpx;
		z-index: 1;
	}

	/* 任务信息卡片样式 */
	.task-info-card {
		background: linear-gradient(135deg, rgba(103,194,58,0.1), rgba(103,194,58,0.05));
		border: 2rpx solid $primary;
		border-radius: 20rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 12rpx 36rpx rgba(103,194,58,0.2);
	}

	.task-info-header {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.task-info-title {
		font-size: 32rpx;
		font-weight: 600;
		color: $primary;
		margin-left: 16rpx;
	}

	.task-info-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 0;
	}

	.info-item {
		flex: 1;
		text-align: center;
	}

	.info-label {
		display: flex;
		align-items: center;
		justify-content: center;
		color: #5a9f3a;
		font-size: 24rpx;
		margin-bottom: 16rpx;
	}

	.info-label text {
		margin-left: 8rpx;
	}

	.info-value {
		color: #2d5016;
		font-size: 28rpx;
		font-weight: 700;
		word-break: break-all;
	}

	.info-divider {
		width: 2rpx;
		height: 88rpx;
		background: rgba(103,194,58,0.35);
		margin: 0 20rpx;
	}

	/* 响应式设计 */
	@media (max-width: 750rpx) {
		.task-info-content {
			flex-direction: column;
		}
		
		.info-divider {
			display: none;
		}
		
		.info-item {
			width: 100%;
			padding: 20rpx 0;
		}
		
		.info-value {
			font-size: 24rpx;
		}
	}

	.tongguo {
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		color: #fff;

		.tg {
			width: 40%;
			display: flex;
			align-items: center;
			justify-content: center;
			background: green;
			height: 80rpx;
			border-radius: 40rpx;
		}

		.bh {
			width: 40%;
			display: flex;
			align-items: center;
			justify-content: center;
			background: red;
			height: 80rpx;
			border-radius: 40rpx;
		}
	}

	.mine-navbar-bg {
		position: fixed;
		top: 0;
		left: 0;
		width: 750rpx;
		z-index: 1;
	}

	/* 驳回弹窗样式 */
	.reject-modal {
		width: 650rpx;
		background: #fff;
		border-radius: 14rpx;
		overflow: hidden;
	}

	.reject-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 28rpx 28rpx 12rpx;
		border-bottom: 1rpx solid $line;
	}

	.reject-header .title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.reject-body {
		padding: 20rpx 28rpx;
	}

	.form-item {
		margin-bottom: 18rpx;
	}

	.label {
		display: inline-block;
		font-size: 26rpx;
		color: #666;
		margin-bottom: 10rpx;
	}

	.label.required::before {
		content: '*';
		color: #f56c6c;
		margin-right: 6rpx;
	}

	.picker-input {
		height: 88rpx;
		border: 1rpx solid $line;
		border-radius: 10rpx;
		padding: 0 24rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.picker-text {
		color: #333;
		font-size: 28rpx;
	}

	.picker-text.placeholder {
		color: #bbb;
	}

	.reject-footer {
		display: flex;
		gap: 16rpx;
		padding: 10rpx 28rpx 24rpx;
	}

	/* 下拉列表样式 */
	.type-item {
		position: relative;
	}

	.dropdown {
		position: absolute;
		left: 0;
		right: 0;
		top: 140rpx;
		background: #fff;
		border: 1rpx solid $line;
		border-radius: 10rpx;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.06);
		z-index: 20;
		max-height: 300rpx;
		overflow: auto;
	}

	.dropdown-item {
		padding: 22rpx 24rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.dropdown-item+.dropdown-item {
		border-top: 1rpx solid $line;
	}

	.di-text {
		font-size: 28rpx;
		color: #333;
	}
</style>